package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.SysUserMapper;
import com.tulele.vtool.mapper.UserGroupGzhMapper;
import com.tulele.vtool.mapper.UserInfoMapper;
import com.tulele.vtool.model.SysUser;
import com.tulele.vtool.model.UserGroupGzh;
import com.tulele.vtool.model.UserGroupOpenId;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.service.SysUserService;
import com.tulele.vtool.service.UserGroupGzhService;
import com.tulele.vtool.wechat.service.WeixinService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统用户(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2021-01-04 16:21:22
 */
@Service("userGroupGzhService")
public class UserGroupGzhServiceImpl implements UserGroupGzhService {

    @Autowired
    private UserGroupGzhMapper userGroupGzhMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserGroupGzh getUserGroupGzh(String gzhCode) {
        return userGroupGzhMapper.getUserGroupGzh(gzhCode);
    }

    @Override
    public List<UserGroupGzh> getUserGroupGzhList(String groupCode) {
        return userGroupGzhMapper.getUserGroupGzhList(groupCode);
    }

    @Override
    public void addUserGroupOpenId(UserGroupOpenId userGroupOpenId) {
        userGroupGzhMapper.addUserGroupOpenId(userGroupOpenId);
    }

    @Override
    public UserGroupOpenId getUserGroupUserId(String gzhCode, String openId) {
        return userGroupGzhMapper.getUserGroupUserId(gzhCode, openId);
    }

    @Override
    public List<UserGroupOpenId> getUserGroupOpenIds(Integer userId) {
        return userGroupGzhMapper.getUserGroupOpenIds(userId);
    }

    @Override
    public UserGroupOpenId getUserOpenId(String gzhCode, Integer userId) {
        return userGroupGzhMapper.getUserOpenId(gzhCode, userId);
    }

    @Override
    public Integer getUserGroupUserId(String springProfilesActive, String openId, String appId, String unionId, String groupCode) {
        Integer groupUserId = null;
        try {
            UserGroupOpenId userGroupOpenId = getUserGroupUserId(springProfilesActive, openId);//先按openid在映射表中找
            if (userGroupOpenId != null && userGroupOpenId.getUserId() != null) {
                groupUserId = userGroupOpenId.getUserId();
            } else {
                UserGroupOpenId userGroupOpenIdByUnionId = userGroupGzhMapper.getUserGroupUserIdByUnionId(groupCode, unionId);//按unionId在映射表中找
                if (userGroupOpenIdByUnionId != null && userGroupOpenIdByUnionId.getUserId() != null) {
                    groupUserId = userGroupOpenIdByUnionId.getUserId();
                } else {
                    UserInfo groupUser = userInfoMapper.queryUserByUnionIdAndGroupCode(unionId, groupCode, springProfilesActive);//通过unionid和公众号组号 去获取 主userid
                    if (groupUser != null && groupUser.getUserId() != null) {
                        groupUserId = groupUser.getUserId();
                    }
                }

                if (groupUserId != null) {
                    UserGroupOpenId newGroupOpenId = new UserGroupOpenId();
                    newGroupOpenId.setGzhCode(springProfilesActive);
                    newGroupOpenId.setAppId(appId);
                    newGroupOpenId.setOpenId(openId);
                    newGroupOpenId.setUserId(groupUserId);
                    newGroupOpenId.setUnionId(unionId);
                    newGroupOpenId.setGroupCode(groupCode);
                    addUserGroupOpenId(newGroupOpenId);
                }
            }
        } catch (Exception e) {

        }
        return groupUserId;
    }

    //更新同公众号做的 其它用户记录的 活跃时间，配合推送项目 取有效数据
    @Override
    @Async
    public Integer updateLastActiveTimeAndPhoneModels(String gzhCode, String unionId) {

        Integer r = 0;
        try {
            UserGroupGzh userGroupGzh = getUserGroupGzh(gzhCode);
            if (userGroupGzh != null && userGroupGzh.getGroupCode() != null) {
                String groupCode = userGroupGzh.getGroupCode();
                List<UserGroupGzh> userGroupGzhs = getUserGroupGzhList(groupCode);
                if (userGroupGzhs != null && userGroupGzhs.size() > 0) {

                    String phoneModel=null;
                    UserGroupOpenId ugo= userGroupGzhMapper.getUserGroupUserIdByUnionId(groupCode,unionId);//获取主用户ID
                    if(ugo!=null && ugo.getUserId()!=null){
                        UserInfo mainUserInfo= userInfoMapper.queryUserBaseInfo(ugo.getUserId());//获取主用户基本信息，主要是 PhoneModel
                        if(mainUserInfo!=null && mainUserInfo.getPhoneModel()!=null && !mainUserInfo.getPhoneModel().equals("")){
                            phoneModel=mainUserInfo.getPhoneModel();
                        }
                    }

                    for (UserGroupGzh ug : userGroupGzhs) {
                        userInfoMapper.updateLastActiveTimeAndPhoneModelByUnionId(ug.getGzhCode(), unionId,phoneModel);
                        r++;
                    }
                }
            }
        } catch (Exception e) {

        }
        return r;
    }
}